package com.gw.business.analysis.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.gw.business.analysis.model.UserTrackDataStatistics;
import com.gw.business.analysis.service.UserTrackDataDetailService;
import com.gw.business.analysis.service.UserTrackDataStatisticsService;
import com.gw.business.common.util.DateUtil;
import com.gw.platform.logger.Log;
/**
 * 
 * 功能描述: 求每个小时不同枚举与来源中各（访问、模拟、真实、咨询、入金）的统计值
 * <p>
 * 创建者:关博
 * </p>
 * <p>
 * 创建时间:2015-03-06
 * </p>
 * <p>
 * 修改者:
 * </p>
 * <p>
 * 修改时间:
 * </p>
 * <p>
 * 修改原因：
 * </p>
 * <p>
 * 审核者:
 * </p>
 * <p>
 * 审核时间:
 * </p>
 * <p>
 * 审核意见：
 * </p>
 *
 */
@Component
public class UserTrackDataStatisticsTask {
	@Resource
	private UserTrackDataDetailService  userTrackDataDetailService;
	
	@Resource
	private UserTrackDataStatisticsService  userTrackDataStatisticsService;
	
	/**单位：秒 
	 * 60分钟 汇总数据范围（后60分钟的数据进行统计）
	 */
	private final int MINUTES = 60;
	
	//@Scheduled(cron = "* 0/16 * * * ?")
	@Scheduled(cron = "0/480 * * * * ?")
    public void InitDate(){
		Log.info(this.getClass(), "UserTrackDataStatisticsTask开始-->");
		
		long start = System.currentTimeMillis();
		
		//后60分钟的数据进行统计
		Date  halfTime = DateUtil.addMinutes(new Date(), -MINUTES*2);
		String strHalfTime = DateUtil.getDateYyyyMMddHH(halfTime);
		
		
		String yymmdd_Date = DateUtil.getDateDayFormat(halfTime);
		String hours = DateUtil.getHours(halfTime);
		
		List<UserTrackDataStatistics> updateList = new  ArrayList<UserTrackDataStatistics>();
		List<UserTrackDataStatistics> insertList = new  ArrayList<UserTrackDataStatistics>();
		
		//原始数据的统计list
		List<UserTrackDataStatistics> countSourceList = userTrackDataDetailService.groupByhourDate(strHalfTime);
		//当前这个时间  同一时间点上Statistics Data 如果出现同一个下小时点 同一个媒介的数据不同要进行更新操作.
		List<UserTrackDataStatistics> sataicTableList = userTrackDataStatisticsService.getHourDate(yymmdd_Date, hours);
		
		Map<String,UserTrackDataStatistics> oldDataMap = new HashMap<String, UserTrackDataStatistics>();
		
		//先把旧的数据存在map中 key以年月日_时间 _媒介
		for (UserTrackDataStatistics obj : sataicTableList) {
			oldDataMap.put(obj.getFormatDataTime() + "_" + obj.getHours() + "_" + obj.getUtmcsr() + "_" + obj.getUtmcmd(), obj);
		}
		
		//循环新数据 ，把要insert  和update的数据分开
		for (UserTrackDataStatistics new_obj : countSourceList) {
			String key = new_obj.getFormatDataTime() + "_" + new_obj.getHours() + "_" + new_obj.getUtmcsr() + "_" + new_obj.getUtmcmd();
			UserTrackDataStatistics  old_obj = oldDataMap.get(key);
			if(old_obj != null){//比对是否值有修改  update list
				UserTrackDataStatistics updateObj = contrastObj(old_obj,new_obj);
				if(updateObj != null){
					updateList.add(updateObj);
				}
				else{
					continue;
				} 
			}else{//放入insert  list
				insertList.add(new_obj);
			}
		}
				
		Log.info(this.getClass(), "updateList-->" + updateList.size());
		Log.info(this.getClass(), "insertList-->" + insertList.size());
		
		
		//update list insert list 进行
		if(updateList.size() >0 ){
			for (UserTrackDataStatistics userTrackDataStatistics : sataicTableList) {
				userTrackDataStatisticsService.update(userTrackDataStatistics);
			}
		}
			
		
		if(insertList.size() >0 )
			userTrackDataStatisticsService.insert(insertList);
		
		Log.info(this.getClass(), "UserTrackDataStatisticsTask结束，执行时间(s)：-->" + (System.currentTimeMillis()-start)/1000);
	}
	
	/**
	 * 前后对象对比。把有修改的值放入 old对象里面  保留id 做更新
	 * @param o
	 * @param n
	 * @return
	 */
	private UserTrackDataStatistics contrastObj(UserTrackDataStatistics o,UserTrackDataStatistics n){
		Boolean update=false;
		if(!o.getVisitCount().equals(n.getVisitCount())){
			o.setVisitCount(n.getVisitCount());
			update=true;
		}
		
		if(!o.getAdvisoryCount().equals(n.getAdvisoryCount())){
			o.setAdvisoryCount(n.getAdvisoryCount());
			update=true;
		}
		
		if(!o.getDemoCount().equals(n.getDemoCount())){
			o.setDemoCount(n.getDemoCount());
			update=true;
		}
		
		if(!o.getRealCount().equals(n.getRealCount())){
			o.setRealCount(n.getRealCount());
			update=true;
		}
		
		if(!o.getDepositCount().equals(n.getDepositCount())){
			o.setDepositCount(n.getDepositCount());
			update=true;
		}
		
		if(update)
			return o ;
		else 
			return null;
		
	}
	
}
